package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Film;
import com.zretc.entity.FilmType;
import com.zretc.service.FilmTypeService;
import com.zretc.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Controller
@RequestMapping("/back")
public class BackFilmTypeController {

    private static final Logger logger = LoggerFactory.getLogger(BackFilmTypeController.class);

    @Autowired
    private FilmTypeService filmTypeService;

    @GetMapping("/filmTypes")
    @ResponseBody
    public PageInfo<FilmType> getFilmTypeListByCondition(@RequestParam("page") String pageNum,
                                                         @RequestParam("limit") String pageSize,
                                                         String filmTypeName) {

        Map<String, String> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("filmTypeName",filmTypeName);
        PageInfo<FilmType> pageInfoFilmType = filmTypeService.getFilmTypeListByCondition(map);

        return pageInfoFilmType;
    }

    @PostMapping("/insertFilmType")
    @ResponseBody
    public boolean insertFilmType(@RequestParam("filmTypeName") String filmTypeName) {

        FilmType filmType = new FilmType();
        filmType.setFilmTypeName(filmTypeName);
        boolean b = filmTypeService.insertFilmType(filmType);
        if (b == true) {
            return true;
        } else {
            return false;
        }

    }

    @DeleteMapping("/deleteFilmType/{filmTypeId}")
    @ResponseBody
    public boolean deleteFilmTypeByFilmTypeId(@PathVariable("filmTypeId")  String typeId) {

        Integer filmTypeId = Integer.valueOf(typeId);
        boolean b = filmTypeService.deleteFilmTypeByFilmTypeId(filmTypeId);
        if (b == true) {
            return true;
        } else {
            return false;
        }
    }

    @PutMapping("/updateFilmType")
    @ResponseBody
    public boolean updateFilmTypeByFilmTypeId(@RequestParam("filmTypeId") String filmTypeId,
                                              @RequestParam("filmTypeName") String filmTypeName) {

        FilmType filmType = new FilmType();
        filmType.setFilmTypeId(Integer.valueOf(filmTypeId));
        filmType.setFilmTypeName(filmTypeName);

        boolean b = filmTypeService.updateFilmTypeByFilmTypeId(filmType);
        if (b == true) {
            return true;
        } else {
            return false;
        }
    }
    @GetMapping("/filmTypeLists")
    @ResponseBody
    public List<FilmType> getfilmTypeList() {

        List<FilmType> filmTypeList = filmTypeService.getfilmTypeList();

        return filmTypeList;
    }

    @GetMapping("/exportFilmType")
    @ResponseBody
    public void fileExportFilmType(HttpServletResponse response) throws IOException {

        // 标题内容（可在前端设置，通过参数传递进来）
        // key是实体类的属性值，value是表头的lable
        Map<String, String> head = new LinkedHashMap<>();
        head.put("filmTypeId","类型编号");
        head.put("filmTypeName","类型名称");

        //表格数据内容-数据集查询出来的数据
        List<FilmType> data = new ArrayList<>();

        List<FilmType> filmTypes = filmTypeService.getfilmTypeList();
        for (FilmType filmType : filmTypes) {
            Integer filmTypeId = filmType.getFilmTypeId();
            String filmTypeName = filmType.getFilmTypeName();
            data.add(new FilmType(filmTypeId,filmTypeName));
        }

        // 生成工作簿
        HSSFWorkbook hssfWorkbook = ExcelUtil.creatExcel(head,data);

        // 定义文件名
        String fileName = "影片类型信息表";

        // 下载
        // 设置响应头
        response.setHeader("Cache-Control","max-age=0");
        // 设置响应类型为excel
        response.setContentType("application/vnd.mx-excel");
        // 设置文件头编码方式和文件名
        response.addHeader("Content-disposition",
                "attachment;filename=" + new String(fileName.getBytes("gb2312"),"ISO-8859-1") + ".xls");
        OutputStream outputStream = response.getOutputStream();
        hssfWorkbook.write(outputStream);
        outputStream.flush();
        outputStream.close();

    }

}
